package com.code.leetcode.offer;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/6 9:58
 */
public class The59 {

    Deque<Integer> deque = new LinkedList<>();

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k == 0) return new int[]{};
        int left = 0, right = 0;
        int[] rtns = new int[nums.length - k + 1];

        for (int j = 0, i = 1 - k; j < nums.length; i++, j++) {
            if (i > 0 && deque.peekFirst() == nums[i - 1]) {
                deque.removeFirst();
            }
            while (!deque.isEmpty() && deque.peekLast() < nums[j]) {
                deque.removeLast();
            }
            deque.addLast(nums[j]);
            if (i >= 0) rtns[i] = deque.peekFirst();
        }
        return rtns;
    }
}
